package com.icarsaas.saas.tenant.cars.customer.model;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.icarsaas.saas.common.enums.StatusEnum;
import com.icarsaas.saas.component.base.DataEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.*;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author xrong120@gmail.com
 * @title: Coupon
 * @projectName icarsaas
 * @description: 优惠券实体
 * @date 2022/1/2211:47
 */
@Data @Entity @Table(name = "cars_coupon")
@DynamicInsert @DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@EntityListeners(AuditingEntityListener.class)
//@org.hibernate.annotations.Table(appliesTo = "cars_coupon",comment="优惠券")
@ApiModel(description = "优惠券")
public class Coupon extends DataEntity {
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "id", nullable = false, insertable = true, updatable = true, length = 36)
    private String id;
    //    @Column(columnDefinition="varchar(50) COMMENT '优惠券类型'")
    @Column(length = 50) @ApiModelProperty(value = "优惠券类型")
    private String type;//代金券、折扣券、消费券
    @Column(length = 100) @ApiModelProperty(value = "优惠券名称")
//    @Column(columnDefinition="varchar(50) COMMENT '优惠券名称'")
    private String name;//优惠券名称
//    @Column(columnDefinition="decimal(10,0) COMMENT '优惠金额/点值'")
    @ApiModelProperty(value = "优惠金额/赠送点值")
    private BigDecimal money;//优惠金额/赠送点值
//    @Column(columnDefinition="decimal(10,2) COMMENT '折扣'")
    @ApiModelProperty(value = "折扣")
    private BigDecimal dis;//折扣
//    @Column(columnDefinition="int COMMENT '发行数量'")
    @ApiModelProperty(value = "发行数量")
    private BigDecimal cnt;//发行数量
//    @Column(columnDefinition="decimal(10,2) COMMENT '使用条件'")
    @ApiModelProperty(value = "使用条件")
    private BigDecimal cond;//使用条件
//    @Column(columnDefinition="datetime COMMENT '生效时间'") @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "生效时间") @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date effDate;//生效时间
//    @Column(columnDefinition="datetime COMMENT '失效时间'") @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "失效时间") @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date expDate;//失效时间

//    @Column(columnDefinition="varchar(4000) COMMENT '可用店铺'")
    @Column(length = 4000) @ApiModelProperty(value = "可用店铺")
    private String userStore;//可用店铺(编号)
//    @Column(columnDefinition="varchar(4000) COMMENT '可用会籍'")
    @Column(length = 4000) @ApiModelProperty(value = "可用会籍")
    private String userMember;//可用会籍(会籍编号)
//    @Column(columnDefinition="tinyint(4) COMMENT '领取规则'")
    @Column(length = 4) @ApiModelProperty(value = "领取规则")
    private Byte gain = 0;//领取规则: 0 不限; 1每人限领一次；2 每人每天限领一次
//    @Column(columnDefinition="tinyint(4) COMMENT '优惠券状态'")
    @Column(length = 4) @ApiModelProperty(value = "状态")
    private Byte status = StatusEnum.OK.getCode();// 优惠券状态，应该在到期后自动失效

    /**
     * 获取优惠券可用店铺列表
     * @return
     */
    public List<XmSelectModel> getUserStoreList () {
        List<XmSelectModel> list = Arrays.asList();
        if (StringUtils.isNotEmpty(userStore)) {
            JSONArray jsonArray = JSONUtil.parseArray(userStore);
            list = JSONUtil.toList(jsonArray, XmSelectModel.class);
        }
        return list;
    }

    public String getUserStoreStr() {
        String str = "";
        List<XmSelectModel> list = getUserStoreList();
        if (list.size() > 0) {
            for (XmSelectModel model : list) {
                if (StringUtils.isNotEmpty(str)) str += ",";
                str += model.getName();
            }
        }
        return str;
    }

    /**
     * 获取优惠券会籍列表
     * @return
     */
    public List<XmSelectModel> getUserMemberList () {
        List<XmSelectModel> list = Arrays.asList();
        if (StringUtils.isNotEmpty(userMember)) {
            JSONArray jsonArray = JSONUtil.parseArray(userMember);
            list = JSONUtil.toList(jsonArray, XmSelectModel.class);
        }
        return list;
    }

    public String getUserMemberStr() {
        String str = "";
        List<XmSelectModel> list = getUserMemberList();
        if (list.size() > 0) {
            for (XmSelectModel model : list) {
                if (StringUtils.isNotEmpty(str)) str += ",";
                str += model.getName();
            }
        }
        return str;
    }
}
